import java.io.*;

public class MP3File implements Serializable{

    private static final long serialVersionUID = 1L;

    public byte[] binaryMP3; 
    public boolean creationSuccess; 
    public String name;

    private MP3File next;
    private MP3File prev;

    public MP3File(File selectedFile) {
	FileInputStream fis = null;
	name = selectedFile.getName();
	try {
	    fis = new FileInputStream(selectedFile);
	}
	catch(FileNotFoundException E){System.out.println("guava");}
	ByteArrayOutputStream buffer = new ByteArrayOutputStream();

	int nRead;
	byte[] data = new byte[262144]; //an arbitrary value for the buffer size: .25 MB. It is large, but most MP3s are 5-10 MB, so it is probably reasonable.
	try {
	    while ((nRead = fis.read(data, 0, data.length)) != -1) {
		buffer.write(data, 0, nRead);
	    }
	    buffer.flush();
	}
	catch(IOException E){System.out.println("kiwi");}
	binaryMP3 = buffer.toByteArray();

	// //the rest of the constructor serializes the instance of MP3File
	// FileOutputStream fos = null;
	// ObjectOutputStream out = null;
	// try
	//     {
	// 	fos = new FileOutputStream(selectedFile.getName());
	// 	out = new ObjectOutputStream(fos);
	// 	out.writeObject(this);
	// 	out.flush();
	// 	out.close();
	//     }
	// catch(IOException ex) {}
    }

    public InputStream getStream() {
	ByteArrayInputStream stream = new ByteArrayInputStream(binaryMP3);
	return (InputStream)stream;
    }

    public String toString() {
	return name;
    }

    public MP3File getNext() {
	return next;
    }

    public MP3File getPrev() {
	return prev;
    }

    public void setNext(MP3File file) {
	next = file;
    }

    public void setPrev(MP3File file) {
	prev = file;
    }

}